package com.muse.designpatterns.chain;

import org.apache.logging.log4j.message.Message;

import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractProcessor {
    //责任链中下一个处理节点
    private AbstractProcessor nextProcessor;

    public final void handleMessage(List<Email> emails) {
        List<Email> filterEmails = emails.stream().filter(email -> email.getType() == this.emailType()).collect(Collectors.toList());
        this.execute(filterEmails);
        if (this.nextProcessor == null) return;
        this.nextProcessor.handleMessage(emails);

    }
    //设置下一个责任链处理器
    public void setNextProcessor(AbstractProcessor processor) {
        this.nextProcessor = processor;
    }
    //获取当前Processor可以处理的邮件类型
    protected abstract int emailType();

    //具体处理邮件的方法
    protected abstract void execute(List<Email> emails);
}
